package com.cloudxingkong.dianyinke.Student.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.WordWrapView;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Ui.ConfirmOrder;
import com.cloudxingkong.dianyinke.Student.Ui.MyAppointment;
import com.cloudxingkong.dianyinke.Student.bean.MyAppointmentEntity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by ningfei on 2016/3/22.
 */
public class MyAppointmentAdapter extends BaseAdapter {
	private List<MyAppointmentEntity.ListBean> datas;
	private Context context;
	private onTouchItemPositions onTouchPosition;

	public MyAppointmentAdapter (List<MyAppointmentEntity.ListBean> datas, Context context, onTouchItemPositions onTouchPosition) {
		this.datas = datas;
		this.context = context;
		this.onTouchPosition = onTouchPosition;


	}


	@Override
	public int getCount () {
		return datas == null ? 0 : datas.size ();
	}

	@Override
	public Object getItem (int position) {
		return datas.get (position);
	}

	@Override
	public long getItemId (int position) {
		return position;
	}

	@Override
	public View getView (final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate (context, R.layout.myappointment_item, null);
			holder = new ViewHolder ();
			//holder.tv_yuyue = (TextView) convertView.findViewById(R.id.tv_yuyue);
			holder.tv_laoshi = (TextView) convertView.findViewById (R.id.tv_laoshi);
			holder.tv_kecheng = (TextView) convertView.findViewById (R.id.tv_kecheng);
			holder.tv_neirong = (TextView) convertView.findViewById (R.id.tv_neirong);
			holder.tv_shijian = (TextView) convertView.findViewById (R.id.tv_shijian);

			holder.tv_zaicigoumia = (TextView) convertView.findViewById (R.id.tv_zaicigoumai);
			holder.tv_querenshiyong = (TextView) convertView.findViewById (R.id.tv_querenshiyong);
			holder.tv_yishiyong = (TextView) convertView.findViewById (R.id.tv_yishiyong);

			holder.ll_distance = (LinearLayout) convertView.findViewById (R.id.ll_myAppointment);
			holder.wwv = ((WordWrapView) convertView.findViewById (R.id.myAppointment_view));
			holder.mdyy_icon = (CircleImageView) convertView.findViewById (R.id.mdyy_icon);
			convertView.setTag (holder);
		} else {
			holder = (ViewHolder) convertView.getTag ();
		}
		onTouchPosition.onTouch (position);
		//holder.tv_yuyue.setText(datas.get(position).getYuyue() + "");
		holder.tv_laoshi.setText (datas.get (position).getMemname () + "");//老师名字
		holder.tv_kecheng.setText (datas.get (position).getTitle () + ""); //课程标题
		holder.tv_neirong.setText (datas.get (position).getNote () + "");  //内存简绍

		//  holder.tv_shijian.setText(datas.get(position).getTime() + "");  //时间
		String serverTime = datas.get (position).getTime () + "";
		String ts = serverTime.substring (0, 10);
		holder.tv_shijian.setText (ts);

		ImageLoader.getInstance ().displayImage (datas.get (position).getAvatar (), holder.mdyy_icon);

		String label = datas.get (position).getLabel () + "";//标签
		if (label != null) {
			String[] split = label.split (",");
			for (int i = 0; i < split.length; i++) {
				TextView tv = new TextView (context);
				tv.setGravity (Gravity.CENTER);
				tv.setTextColor (context.getResources ().getColor (R.color.main));
				tv.setBackgroundResource (R.drawable.shape_rectangle_white_solid_btn);
				tv.setText (split[i]);
				if (holder.wwv.getChildCount () < split.length) {
					holder.wwv.addView (tv);
				}
				//Log.e("test", position + " -- " + holder.wwv.getChildCount() + " -- " + split.length + " -- " + split[i] + " --- " + tv.getText());
			}
		} else {
			Log.e ("test", "标签为空");
		}

		int isuse = datas.get (position).getIsuse ();
		if (isuse == 0) {  //未使用
			holder.tv_zaicigoumia.setVisibility (View.VISIBLE);
			holder.tv_yishiyong.setVisibility (View.GONE);

			//确认使用
			final ViewHolder finalHolder1 = holder;
			holder.tv_querenshiyong.setOnClickListener (new View.OnClickListener () {
				@Override
				public void onClick (final View v) {
					new AlertDialog.Builder (context)
							.setTitle ("确定使用")
							.setMessage ("点击确认使用以后，我们将货款发给教您的老师,欢迎您的下次购买！")//设置显示的内容
							.setPositiveButton ("确定", new DialogInterface.OnClickListener () {
								@Override
								public void onClick (DialogInterface dialog, int which) {//确定按钮的响应事件
									int id = datas.get (position).getId ();
									onTouchPosition.onUse (id, finalHolder1.tv_querenshiyong, finalHolder1.tv_yishiyong);
								}
							})
							.setNegativeButton ("返回", new DialogInterface.OnClickListener () {//添加返回按钮

								@Override
								public void onClick (DialogInterface dialog, int which) {//响应事件
									Log.i ("alertdialog", " 请保存数据！");
								}

							}).show ();//在按键响应事件中显示此对话框
				}
			});

		} else if (isuse == 1) {  //已使用
			holder.tv_querenshiyong.setVisibility (View.GONE);
			holder.tv_yishiyong.setVisibility (View.VISIBLE);
		}

		final ViewHolder finalHolder = holder;
		//再次够买
		holder.tv_zaicigoumia.setOnClickListener (new View.OnClickListener () {
			@Override
			public void onClick (View v) {
				new AlertDialog.Builder (context)
						.setTitle ("再次购买")
						.setMessage ("您确定要再次购买我们的产品吗？")//设置显示的内容
						.setPositiveButton ("确定", new DialogInterface.OnClickListener () {
							@Override

							public void onClick (DialogInterface dialog, int which) {//确定按钮的响应事件

//                                Toast.makeText(context, "再次够买", Toast.LENGTH_SHORT).show();
								Intent entity = new Intent (context, ConfirmOrder.class);
								entity.putExtra ("teacherPic", datas.get (position).getAvatar () + "");//老师头像
								entity.putExtra ("title", datas.get (position).getTitle () + "");//标题
								entity.putExtra ("content", datas.get (position).getNote () + "");//内容
								entity.putExtra ("price", datas.get (position).getPrice () + "");//单价
								int teacherId = datas.get (position).getTeacherid ();
								entity.putExtra ("teacherID", teacherId + "");//老师ID
								int lessonId = datas.get (position).getLessonid ();
								entity.putExtra ("lessonID", lessonId + "");//订单ID
								((MyAppointment) context).startActivity (entity);
							}

						}).setNegativeButton ("取消", new DialogInterface.OnClickListener () {//添加返回按钮


					@Override

					public void onClick (DialogInterface dialog, int which) {//响应事件

						Log.i ("alertdialog", " 请保存数据！");
					}

				}).show ();//在按键响应事件中显示此对话框
			}
		});

		return convertView;
	}

	private class ViewHolder {
		TextView tv_laoshi, tv_kecheng, tv_neirong, tv_shijian, tv_zaicigoumia, tv_querenshiyong, tv_yishiyong;
		LinearLayout ll_distance;
		WordWrapView wwv;
		CircleImageView mdyy_icon;
	}

	public interface onTouchItemPositions {
		void onTouch (int position);

		void onUse (int id, TextView btn, TextView text);
	}

}
